package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCartBean;
import com.project.bean.ProductBean;
import com.project.bean.SellCartBean;
import com.project.bean.SellOrderBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISellCartMapper;
import com.project.mapper.ISellDetailMapper;
import com.project.mapper.ISellOrderMapper;
import com.project.service.ISellCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SellCartServiceImpl implements ISellCartService {
    @Autowired
    private ISellCartMapper sellCartMapper;
    @Autowired
    private ISellOrderMapper sellOrderMapper;
    @Autowired
    private ISellDetailMapper sellDetailMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public List<SellCartBean> findSellCartByUserId(Integer userId) {
        return sellCartMapper.findSellCartByUserId(userId);
    }

    @Override
    public void addSellCart(Integer userId, ProductBean product) {
        QueryWrapper<SellCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        qw.eq("fk_productId", product.getProductId());
        SellCartBean sellCart = sellCartMapper.selectOne(qw);
        if (sellCart != null) {
            sellCart.setSellNum(sellCart.getSellNum() + 1);
            sellCartMapper.updateById(sellCart);
        } else {
            SellCartBean sellCartBean = new SellCartBean(1, product.getProductId(), userId);
            sellCartMapper.insert(sellCartBean);
        }
    }

    @Override
    public void updateSellNum(Integer sellCartId, Integer newSellNum) {
        SellCartBean sellCart = sellCartMapper.selectById(sellCartId);
        sellCart.setSellNum(newSellNum);
        sellCartMapper.updateById(sellCart);
    }

    @Override
    public void delSellCart(Integer sellCartId) {
        sellCartMapper.deleteById(sellCartId);
    }

    @Override
    public void out(Integer userId, String cName, String sellAddr) {
        Integer sellTotal = sellOrderMapper.computeSellTotal(userId);
        SellOrderBean sellOrder = new SellOrderBean(cName, sellAddr, sellTotal, userId);
        sellOrderMapper.insert(sellOrder);
        List<SellCartBean> sellCartList = sellCartMapper.findSellCartByUserId(userId);
        sellDetailMapper.addSellDetail(sellOrder.getSellOrderId(), sellCartList);
        for (int i = 0; i < sellCartList.size(); i++) {
            SellCartBean sellCart = sellCartList.get(i);
            ProductBean product = productMapper.selectById(sellCart.getProductId());
            product.setStockNum(product.getStockNum() - sellCart.getSellNum());
            productMapper.updateById(product);
        }
        QueryWrapper<SellCartBean> qw = new QueryWrapper<>();
        qw.eq("fk_userId", userId);
        sellCartMapper.delete(qw);
    }
}
